<?php

namespace QF\Rbac;

use Treenode;

class Role extends \TreeModel {
    
    use \State;
    
    static public $table = "rbac_role";
    protected $_fillable = array(
        'title' => 'str',
        'comment' => 'str',
        'state' => 'int',
        'sort' => 'int',
        'parent_id' => 'int',
        'route_ids' => 'str',
        'depth' => 'int',
        'parents' => 'str',
        'parents_reverse' => 'str',
    );
    
    const STATE_OPEN = 0;
    const STATE_LOCK = 1;
    
    protected $_states = [
        self::STATE_OPEN => '开放',
        self::STATE_LOCK => '锁定',
    ];

    public function add($data) {
        if ($data['route_ids']) {
            $data['route_ids'] = implode(',', $data['route_ids']) . ',';
        } else {
            $data['route_ids'] = '';
        }
        return parent::add($data);
    }

    public function update($id, $data) {
        if ($data['route_ids']) {
            $data['route_ids'] = implode(',', $data['route_ids']) . ',';
        } else {
            $data['route_ids'] = '';
        }
        return parent::update($id, $data);
    }

    public function getRouteIds($ids) {
        if (!$ids) {
            return array();
        }
        $where = array('id' => array('in' => $ids));
        $route_ids_arr = Db()->getCol(static::$table)->fields('route_ids')->where($where)->result();
        $perms = array();
        if ($route_ids_arr) {
            foreach ($route_ids_arr as $route_ids) {
                if ($route_ids == 'all') {
                    return 'all';
                }
                $perms = array_merge($perms, explode(',', trim($route_ids, ',')));
            }
        }
        return $perms;
    }

    public function getTitles($ids) {
        if (!$ids) {
            return '';
        }
        $where = array('id' => array('in' => $ids));
        $titles = Db()->getCol(static::$table)->fields('title')->where($where)->result();
        if (!$titles) {
            return '';
        }
        return implode(',', $titles);
    }

    /**
     * 获取带checked属性的权限树
     * @return \Treenode
     */
    public function getCheckedTree($role_ids = array(), $indent = '└─ ') {

        if (!is_array($role_ids)) {
            $role_ids = explode(',', trim($role_ids, ','));
        }

        $rows = Db()->getAll(static::$table)->order('depth, parent_id, sort, id')->result();
        
        $tree = new Treenode();

        foreach ($rows as $row) {
            if (in_array($row['id'], $role_ids)) {
                $row['checked'] = 'checked="checked"';
            }
            if ($parent = $tree->getNodeByKey($row['parent_id'])) {
                if ($indent) {
                    $repeat_str = $parent->getLevel() ? str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $parent->getLevel() - 1) . $indent : '';
                    $row['title'] = $repeat_str . $row['title'];
                }
                $parent->addChildNode(new Treenode($row['id'], $row['title'], $row));
            } else {
                $tree->addChildNode(new Treenode($row['id'], $row['title'], $row));
            }
        }

        return $tree->getChildNodes();
    }

}
